<template>
  <div class="sysdept">
    <template>
      <template>
        <el-button type="success" @click="toEdit(0)">新增</el-button>
        <el-button type="primary" @click="toEdit(1)">修改</el-button>
        <el-button type="warning" @click="delById">删除</el-button>
      </template>
      <el-table
        :data="tableData"
        :tree-props="{children: 'sysDeptList', hasChildren: 'hasChildren'}"
        border
        row-key="deptId"
        style="width: 100%;margin-bottom: 20px;"
        @selection-change="handleSelectionChange"
      >
        <el-table-column
          type="selection"
          width="55"
        />
        <el-table-column
          type=""
          label="部门ID"
          prop="deptId"
        />
        <el-table-column
          label="部门名称"
          prop="name"
        />
        <el-table-column
          label="上级部门"
          prop="parentName"
        />
        <el-table-column
          label="联系人"
          prop="contacts"
        />
        <el-table-column
          label="排序号"
          prop="orderNum"
        />
      </el-table>
    </template>
    <!--  //////  表格  //////  -->

  </div>
</template>

<script>
export default {
  name: 'SysDept',
  data() {
    return {
      deptIds: [],
      tableData: []
    }
  },
  created() {
    this.initData()
  },
  methods: {
    delById() {
      if (this.deptIds != null && this.deptIds.length > 0) {
        if (this.deptIds.length > 1) {
          alert('选择数量大于一,请重新选择')
          return
        } else {
          if (confirm('确定删除?')) {
            var deptId = this.deptIds.map(row => row.deptId)
            const number = parseInt(deptId)
            this.axios.get('http://localhost:9999/system-management/sysDept/deleteDept', {
              params: {
                id: number
              }
            }).then((response) => {
              console.log(response.data)
              if (response.data.code === '200') {
                this.initData()
                alert('删除成功')
              } else {
                alert(response.data.msg)
              }
            })
          }
        }
      } else {
        alert('请选择数据后操作')
      }
    },
    toEdit(row) {
      var $this = this
      if (row) {
        if (this.deptIds != null && this.deptIds.length > 0) {
          if (this.deptIds.length > 1) {
            alert('只能修改一条')
            return
          } else {
            var deptId = this.deptIds[0].deptId
            // $this.$router.replace({path:"/edit"})
            $this.$router.push({
              path: 'Edit',
              query: {
                deptId: deptId
              }
            })
            // localStorage.setItem('deptId', deptId)
          }
        } else {
          alert('请选择数据后操作')
        }
      } else {
        // this.$router.replace({path:"/edit"})
        $this.$router.push({
          path: 'DeptEdit',
          query: {
            menuId: 0
          }
        })
        // localStorage.setItem('deptId', 0)
      }
    },
    handleSelectionChange(val) {
      this.deptIds = val
    },
    initData() {
      this.axios.get('http://localhost:9999/system-management/sysDept/list').then((response) => {
        console.log(response.data)
        if (response.data.obj != null) {
          this.tableData = response.data.obj
        }
      })
    }
  }
}
</script>

<style scoped>

</style>
